package com.easonji.admin.controller;

import javax.annotation.Resource;
import javax.validation.Valid;

import com.easonji.admin.form.PermissionForm;
import com.easonji.admin.query.PermissionQuery;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.easonji.admin.manager.AppManager;
import com.easonji.admin.manager.PermissionManager;
import com.easonji.admin.service.PermissionService;
import com.easonji.common.base.controller.BaseController;
import com.easonji.common.base.response.Response;
import com.easonji.common.base.response.enums.ResponseEnum;
import com.easonji.common.constant.Consts;
import com.easonji.common.constant.dictmap.PermissionDict;
import com.easonji.common.util.StringUtils;
import com.easonji.core.annotion.BussinessLog;
import com.easonji.core.annotion.Permission;
import com.easonji.core.log.LogObjectHolder;

/**
 * @ClassName: PermissionController
 * @Description: 权限控制器
 * @Author: LiuJun
 * @date: 2018/07/30 14:07:16
 * @Version: 1.0
 * @Copyright: Copyright (c) 2018 - 2019
 */
@Controller
@RequestMapping("/permission")
public class PermissionController extends BaseController {

    /**
     * 页面前缀
     */
    private final static String PREFIX = "system/permission/";

    @Resource
    private PermissionService permissionService;

    @Resource
    private PermissionManager permissionManager;

    @Resource
    private AppManager appManager;

    /**
     * 跳转到权限首页
     */
    @GetMapping("")
    public String index(Model model) {
        return PREFIX + "permission";
    }

    /**
     * 跳转到添加权限页面
     */
    @GetMapping("/to_add")
    public String toAdd(Model model) {
        return PREFIX + "permission_add";
    }

    /**
     * 跳转到修改权限页面
     */
    @GetMapping("/to_update/{permissionId}")
    public String toUpdate(@PathVariable Integer permissionId, Model model) {
        Object result = permissionService.queryPermission(permissionId);
        LogObjectHolder.me().set(result);
        model.addAttribute("permission", result);
        return PREFIX + "permission_edit";
    }

    /**
     * 查询权限列表
     */
    @ResponseBody
    @PostMapping("/list")
    public Object queryPermissionList(PermissionQuery permissionQuery) {
        Object result = permissionService.queryPermissionList(permissionQuery);
        return result;
    }

    /**
     * 添加权限
     */
    @ResponseBody
    @Permission(Consts.ADMIN_ROLE_NAME)
    @BussinessLog(value = "添加权限", key = "name", dict = PermissionDict.class)
    @PostMapping("/add")
    public Response addPermission(@Valid PermissionForm permissionForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return failure(ResponseEnum.PARAMETER_ERROR);
        }
        int result = permissionService.savePermission(permissionForm);
        if (result == 0) {
            return failure(ResponseEnum.DATABASE_ERROR);
        }
        return success(ResponseEnum.SAVE_SUCCESS);
    }

    /**
     * 修改权限
     */
    @ResponseBody
    @Permission(Consts.ADMIN_ROLE_NAME)
    @BussinessLog(value = "修改权限", key = "name", dict = PermissionDict.class)
    @PostMapping("/update")
    public Response updatePermission(@Valid PermissionForm permissionForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return failure(ResponseEnum.PARAMETER_ERROR);
        }
        int result = permissionService.updatePermission(permissionForm);
        if (result == 0) {
            return failure(ResponseEnum.DATABASE_ERROR);
        }
        return success(ResponseEnum.UPDATE_SUCCESS);
    }

    /**
     * 删除权限
     */
    @ResponseBody
    @Permission(Consts.ADMIN_ROLE_NAME)
    @BussinessLog(value = "删除权限", key = "name", dict = PermissionDict.class)
    @PostMapping("/remove")
    public Response removePermission(@RequestParam Integer permissionId) {
        if (StringUtils.isEmpty(permissionId)) {
            return failure(ResponseEnum.PARAMETER_ERROR);
        }
        String appName = permissionManager.getAppNameById(permissionId);
        String roleName = permissionManager.getPermissionNameById(permissionId);
        LogObjectHolder.me().set(appName + Consts.HYPHEN + roleName);
        permissionService.removePermission(permissionId);
        return success(ResponseEnum.REMOVE_SUCCESS);
    }
}
